package com.cn.web.user.controller;

import cn.hutool.core.util.DesensitizedUtil;
import com.cn.annoation.Logs;
import com.cn.global.GlobalParamterUtil2;
import com.cn.msg.ResultMsg;
import com.cn.page.PageVo;
import com.cn.user.UserDO;
import com.cn.util.AuthUserContext;
import com.cn.util.BaseController;
import com.cn.util.ExcelExportUtil;
import com.cn.web.user.service.UserService;
import com.cn.web.user.vo.PassRestEditDTO;
import com.cn.web.user.vo.UserEditDTO;
import com.cn.web.user.vo.UserSearch;
import com.cn.web.user.vo.UserVO;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * description: UserController <br>
 * date: 21.12.14 11:07 <br>
 * author: cn_yaojin <br>
 * version: 1.0 <br>
 */
@RestController
@RequestMapping(value = "admin/user")
@Api(tags = "用户中心--用户管理")
public class UserController extends BaseController {

    @Autowired
    private UserService userService;

    @Logs(value = "编辑", desc = "用户中心")
    @ApiOperation(value = "编辑")
    @PostMapping(value = "edit")
    public ResultMsg edit(
            @RequestBody @Validated UserEditDTO userEdit
    ) {
        var pd = this.userService.edit(userEdit);
        return ResultMsg.builder().setData(pd);
    }


    @ApiOperation(value = "分页列表")
    @PostMapping(value = "page")
    public ResultMsg<PageInfo<UserVO>> page(
            @RequestBody PageVo<UserSearch> pageVo
    ) {
        PageInfo<UserVO> page = null;
        page = this.userService.page(pageVo);
        return ResultMsg.builder().setData(page);
    }


    @ApiOperation(value = "查看")
    @GetMapping(value = "find")
    public ResultMsg<UserEditDTO> find(
            @ApiParam(name = "id") @RequestParam String id
    ) {
        UserEditDTO info = null;
        info = this.userService.find(id);
        return ResultMsg.builder().setData(info);
    }


    @Logs(value = "删除", desc = "用户中心")
    @ApiOperation(value = "删除")
    @GetMapping(value = "del")
    public ResultMsg del(
            @ApiParam(name = "id") @RequestParam Long id
    ) {
        this.userService.del(id);
        return ResultMsg.builder();
    }


    @Logs(value = "启用停用", desc = "用户中心")
    @ApiOperation(value = "启用停用")
    @GetMapping(value = "start")
    public ResultMsg start(
            @ApiParam(name = "id") @RequestParam Long id,
            @ApiParam(name = "status", value = "0-启用中 1-停用中") @RequestParam String status
    ) {
        this.userService.start(id, status);
        return ResultMsg.builder();
    }


    @Logs(value = "重置密码", desc = "用户中心")
    @ApiOperation(value = "重置密码")
    @GetMapping(value = "restPass")
    public ResultMsg restPass(
            @ApiParam(name = "id") @RequestParam Long id
    ) {
        return ResultMsg.builder().setData(this.userService.restPass(id));
    }


    @Logs(value = "修改密码", desc = "个人中心")
    @ApiOperation(value = "个人中心-修改密码")
    @PostMapping(value = "updatePass")
    public ResultMsg updatePass(
            @RequestBody @Validated PassRestEditDTO passRestEdit
    ) {
        this.userService.updatePass(passRestEdit);
        return ResultMsg.builder();
    }


    @ApiOperation(value = "个人中心-查看个人资料")
    @GetMapping(value = "findUser")
    public ResultMsg<UserEditDTO> findUser() {
        UserDO token = AuthUserContext.get();
        UserEditDTO edit = null;
        edit = this.userService.find(token.getId().toString());
        return ResultMsg.builder().setData(edit);
    }


    @ApiOperation(value = "个人中心-获取个人头像")
    @GetMapping(value = "findUserImg")
    public ResultMsg findUserImg() {
        UserDO token = AuthUserContext.get();
        UserEditDTO edit = null;
        edit = this.userService.find(token.getId().toString());
        if (StringUtils.isNotEmpty(edit.getPhoto())) {
            edit.setPhoto(GlobalParamterUtil2.get().getFileDomain() + edit.getPhoto());
        }
        return ResultMsg.builder(edit.getPhoto());
    }


    @ApiOperation(value = "搜索用户")
    @GetMapping(value = "searchUser")
    public ResultMsg searchUser(
            @ApiParam(name = "name", value = "用户名") @RequestParam String name
    ) {
        UserSearch search = new UserSearch();
        search.setUserName(name);
        return ResultMsg.builder().setData(this.userService.searchUser(search));
    }


    @ApiOperation(value = "导出")
    @PostMapping(value = "export")
    public void export(
            @RequestBody PageVo<UserSearch> pageVo
    ) {
        var page = this.userService.page(pageVo);
        if (page.getList() != null) {
            var list = page.getList();
            for (UserVO userVO : list) {
                userVO.setPhone(DesensitizedUtil.mobilePhone(userVO.getPhone()));
            }
            ExcelExportUtil.export1(response, "管理员", "管理员", UserVO.class, list);
        }
    }

}
